package com.summer.peony.myinterface.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.summer.peony.common.entity.AjaxResult;
import com.summer.peony.common.entity.AjaxResult.Type;
import com.summer.peony.myinterface.pojo.MyInterface;
import com.summer.peony.myinterface.service.MyInterfaceService;

/**
 * @ClassName: MyInterfaceController
 * @Description: TODO(接口管理控制层)
 * @author: TaoGe
 * @date: 2020年11月4日 上午10:50:43
 * @version: v0.0.1
 */
@RestController
@RequestMapping("/interface")
public class MyInterfaceController {

	@Autowired
	private MyInterfaceService myInterfaceService;

	/**
	 * 新增接口案例
	 * 
	 * @Description: TODO(描述这个方法的作用)
	 * @param myInterface
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年11月4日 下午1:53:46
	 * @throws:
	 */
	@PostMapping("/addInterface")
	public AjaxResult addInterface(@RequestBody MyInterface myInterface) {
		AjaxResult ajaxResult = new AjaxResult();
		Integer myInf = myInterfaceService.addInterface(myInterface);
		if (myInf == 1) {
			ajaxResult.put("code", 0);
			ajaxResult.put("msg", "添加成功");
			return ajaxResult;
		}
		ajaxResult.put("code", 999999);
		ajaxResult.put("msg", "添加失败");
		return ajaxResult;
	}

	/**
	 * 修改接口案例
	 * 
	 * @Description: TODO(描述这个方法的作用)
	 * @param myInterface
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年11月4日 下午1:53:59
	 * @throws:
	 */
	@PutMapping("/updateInterface")
	public AjaxResult updateInterface(@RequestBody MyInterface myInterface) {
		AjaxResult ajaxResult = new AjaxResult();
		Integer myInf = myInterfaceService.updateInerface(myInterface);
		if (myInf == 1) {
			ajaxResult.put("code", 0);
			ajaxResult.put("msg", "修改成功");
			return ajaxResult;
		}
		ajaxResult.put("code", 999999);
		ajaxResult.put("msg", "修改失败");
		return ajaxResult;
	}

	/**
	 * 删除接口案例
	 * 
	 * @Description: TODO(描述这个方法的作用)
	 * @param id
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年11月4日 下午2:04:12
	 * @throws:
	 */
	@DeleteMapping("/deleteInterface/{id}")
	public AjaxResult deleteInterface(@PathVariable("id") Integer id) {
		Integer myInf = myInterfaceService.deleteInterface(id);
		if (myInf == 1) {
			return new AjaxResult(Type.SUCCESS, "删除成功");
		}
		return new AjaxResult(Type.ERROR, "删除失败");
	}

	/**
	 * 分页查询接口列表
	 * 
	 * @Description: TODO(描述这个方法的作用)
	 * @param id
	 * @param name
	 * @param result
	 * @param pageNum
	 * @param pageSize
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2020年11月4日 下午3:39:09
	 * @throws:
	 */
	@GetMapping("/selectAllInterface")
	public AjaxResult selectAllInterface(Integer id, String name, Integer result, Integer pageNum, Integer pageSize) {
		PageInfo<MyInterface> pageInfo = myInterfaceService.selectAllInterface(id, name, result, pageNum, pageSize);
		if (pageInfo != null) {
			return new AjaxResult(Type.SUCCESS, "查询成功", pageInfo);
		}
		return new AjaxResult(Type.ERROR, "查询失败");
	}
}
